package com.dang.alex.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.dang.alex.model.User;
import com.dang.alex.service.FactoryService;
import com.dang.alex.service.Userservice;

/**
 * Servlet implementation class ValDateEmailServlet
 */
@WebServlet("/ValDateEmailServlet")
public class ValDateEmailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private  boolean flag=false;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email=request.getParameter("email");
		FactoryService fac=new FactoryService();
		Userservice userimp=fac.getUserDAO();
		JSONObject json=new JSONObject();
		try {
			User user=userimp.findByEmail(email);
			if(user==null) {
				flag=true;
			}else {	
				flag=false;
			}
			json.put("flag", flag);
			PrintWriter out=response.getWriter();
			out.println(json.toString());
			out.close();	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
			
	}

}
